package kercar.launcher;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import kercar.debug.Debugger;
import kercar.device.MasterChief;
import kercar.device.RemoteDevice;
import kercar.manager.CompassManager;
import kercar.manager.GPSManager;
import kercar.manager.MailManager;
import kercar.manager.MapManager;
import kercar.manager.MovementManager;
import kercar.manager.PhotoManager;
import kercar.manager.WebManager;
import kercar.robot.ArduinoRobot;
import kercar.robot.Robot;

public class Launcher {

	/************************************************************************* BLOCK
	 * ATTRIBUTES
	 *******************************************************************************/
	
	public static final String IP = "127.0.0.1";
	public static final int PORT = 1099;
	public static final String NAME = "MasterChief";
	
	/************************************************************************* BLOCK
	 * MAIN METHODS
	 *******************************************************************************/
	
	public static void main(String[] args) {
		try {
			Robot robot = new ArduinoRobot();
			RemoteDevice mc = new MasterChief();
			
			MovementManager movManager = new MovementManager();
			movManager.setRobot(robot);
			GPSManager gpsManager = new GPSManager();
			CompassManager compManager = new CompassManager();
			MapManager mapManager = new MapManager();
			PhotoManager photoManager = new PhotoManager();
			WebManager webManager = new WebManager();
			MailManager mailManager = new MailManager();
			
			((MasterChief) mc).setMovementManager(movManager);
			((MasterChief) mc).setGPSManager(gpsManager);
			((MasterChief) mc).setCompassManager(compManager);
			((MasterChief) mc).setMapManager(mapManager);
			((MasterChief) mc).setPhotoManager(photoManager);
			((MasterChief) mc).setConnectToWeb(webManager);
			((MasterChief) mc).setMailManager(mailManager);
			
			((ArduinoRobot) robot).setGPSManager(gpsManager);
			((ArduinoRobot) robot).setCompassManager(compManager);
			
			// RMI
			LocateRegistry.createRegistry(PORT);
			Debugger.logMsg("RMI Registry created on port " + PORT);
			
			Registry registry = LocateRegistry.getRegistry(IP);
			registry.rebind(NAME, mc);
			Debugger.logMsg("RMI Object registered");
			
			// starting Arduino listener
			((ArduinoRobot) robot).startListener();
		} catch (Exception e) {
			Debugger.logException(e);
		}
	}

}